<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <canvas id="canvas" height="600" width="700"></canvas>
  <script>
    const canvas = document.getElementById('canvas')
    const ctx = canvas.getContext('2d')
    // 垂直方向对齐为top:
    ctx.beginPath()
    ctx.font = '30px Verdana'
    ctx.textAlign = 'center'
    ctx.textBaseline = 'top'
    ctx.fillText('Hello Canvas!', 120, 300, 200)
    // 垂直方向对齐为middle:
    ctx.beginPath()
    ctx.textBaseline = 'middle'
    ctx.fillText('Hello Canvas!', 350, 300, 200)
    // 垂直方向对齐为bottom:
    ctx.beginPath()
    ctx.textBaseline = 'bottom'
    ctx.fillText('Hello Canvas!', 600, 300, 200)

    // 一条水平过文本起始点的辅助线：
    ctx.beginPath()
    ctx.setLineDash([10])
    ctx.globalAlpha = 0.5
    ctx.moveTo(0, 300)
    ctx.lineTo(700, 300)
    ctx.stroke()
  </script>
</body>

</html>